﻿using System.Collections.Generic;
using System.IO;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.ZLog;

namespace UtilZ.Dotnet.Ex.Communication.Net.Web
{
    /// <summary>
    /// 网站配置
    /// </summary>
    public class WebSiteConfig
    {
        private string _rootDir = null;
        /// <summary>
        /// 网页根目录
        /// </summary>
        public string RootDir
        {
            get { return _rootDir; }
            set { _rootDir = value; }
        }

        /// <summary>
        /// 默认页面文件路径
        /// </summary>
        private string _defaultPageFilePath = null;
        /// <summary>
        /// 默认页面
        /// </summary>
        public string DefaultPageFilePath
        {
            get { return _defaultPageFilePath; }
            set { _defaultPageFilePath = value; }
        }

        private IEnumerable<string> _sourceDirs = null;
        /// <summary>
        /// 资源目录集合
        /// </summary>
        public IEnumerable<string> SourceDirs
        {
            get { return _sourceDirs; }
            set { _sourceDirs = value; }
        }

        /// <summary>
        /// url请求资源缓存时长,单位:毫秒
        /// </summary>
        public long UrlCacheMilliseconds { get; set; } = 1000 * 60 * 10;

        /// <summary>
        /// web站点资源提供器
        /// </summary>
        public IWebSiteResourceProvider ResourceProvider { get; set; } = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        public WebSiteConfig()
        {

        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rootDir">网页根目录</param>
        /// <param name="defaultPage">默认页面</param>
        /// <param name="sourceDirs">可能存放资源目录集合</param>
        public WebSiteConfig(string rootDir, string defaultPage = null, IEnumerable<string> sourceDirs = null)
            : base()
        {
            if (string.IsNullOrWhiteSpace(rootDir))
            {
                rootDir = PathEx.GetTypeAssemblyDirectory(this.GetType());
            }
            this._rootDir = rootDir;

            if (!string.IsNullOrWhiteSpace(defaultPage))
            {
                if (string.IsNullOrWhiteSpace(Path.GetPathRoot(defaultPage)))
                {
                    this._defaultPageFilePath = PathEx.Combine(PathType.Local, rootDir, defaultPage);
                }
                else
                {
                    this._defaultPageFilePath = defaultPage;
                }

                if (!File.Exists(this._defaultPageFilePath))
                {
                    ZLoger.Warn($"默认页面文件\"{this._defaultPageFilePath}\"不存在");
                }
            }

            this._sourceDirs = sourceDirs;
        }
    }
}
